//
//  YNRemindManager.m
//  Messenger
//
//  Created by OC on 15/6/9.
//  Copyright (c) 2015年 YN-APP-iOS. All rights reserved.
//

#import "YNRemindManager.h"
#import "MessageRemind.h"
#import "YNIMFrameworkMacro.h"
#import "XMPPDBManager.h"
#import "RemindSet.h"
#import "YNContactGroup.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "FMDatabaseAdditions.h"

@implementation YNRemindManager

static FMDatabaseQueue *_queue;

//初始化消息提醒信息
+ (void)initMessageRemindData
{
    _queue = [[XMPPDBManager sharedInstance] achieveQueue];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [_queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
            
            NSArray *remindSetArray = [RemindSet queryInDatabase:db];
            
            if ([remindSetArray count] == 0)
            {
                //如果消息提醒设置为空，则插入初始值
                RemindSet *remindSet = [[RemindSet alloc] init];
                remindSet.DistractionFree_startTime = @"23:00";
                remindSet.DistractionFree_endTime = @"07:00";
                
                //2017-5-18jc修改，默认设置有震动，有声音提醒
                //没有免打扰功能
                remindSet.isVibrate = 0;
                remindSet.isGroupVibrate = 0;
                remindSet.isAudio  = 1;
                remindSet.isGroupAudio = 1;
                
                remindSet.isDistractionFree = 0;
                
                [remindSet insertInDatabase:db];
            }
        }];
    });

}

/**
 *  @author 李健, 15-06-15
 *
 *  @brief  查询群/讨论组 消息提醒信息
 *
 *  @return 返回一个字典，里边有两个值 key:@"REMIND" 消息提醒 key:@"DISREMIND" 不提醒
 */
+ (NSDictionary *)queryGroupRemind
{
      _queue = [[XMPPDBManager sharedInstance] achieveQueue];
    
      NSMutableArray *remindArray = [NSMutableArray array];
      NSMutableArray *disRemindArray = [NSMutableArray array];
      __block NSArray *groupRemindArray;
      __block NSArray *disGroupRemindArray;
      __block NSArray *groupDisRemindArray;
      __block NSArray *disGroupDisRemindArray;
      [_queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
          
          groupRemindArray = [MessageRemind queryGroupRemind:1 database:db];
          disGroupRemindArray = [MessageRemind queryDisGroupRemind:1 database:db];
          groupDisRemindArray = [MessageRemind queryGroupRemind:0 database:db];
          disGroupDisRemindArray = [MessageRemind queryDisGroupRemind:0 database:db];
          
      }];

     [remindArray addObjectsFromArray:groupRemindArray];
     [remindArray addObjectsFromArray:disGroupRemindArray];
     [disRemindArray addObjectsFromArray:groupDisRemindArray];
     [disRemindArray addObjectsFromArray:disGroupDisRemindArray];
    
     NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
    
     [resultDic setObject:remindArray forKey:@"REMIND"];
     [resultDic setObject:disRemindArray forKey:@"DISREMIND"];
    
      return resultDic;

}


+ (BOOL)saveNewGroupRemind:(NSString *)groupId
{
    _queue = [[XMPPDBManager sharedInstance] achieveQueue];
    
    __block  BOOL result = NO;

    MessageRemind *remind = [[MessageRemind alloc] init];
    remind.isRemind = 1;
    remind.groupID = groupId;
    
    [_queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        if ([remind isMessageRemindExistInDatabase:db])
        {
            
            result = [remind updateInDatabase:db];
        }
        else
        {
            result = [remind insertInDatabase:db];
        
        }
        
    }];
    
    return result;
}


//更新群/讨论组消息提醒信息
+ (BOOL)updateMessageRemind:(MessageRemind *)messageRemind
{
    _queue = [[XMPPDBManager sharedInstance] achieveQueue];
    __block  BOOL result = NO;
    [_queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
          
        if ([messageRemind isMessageRemindExistInDatabase:db])
        {
            result = [messageRemind updateInDatabase:db];
        };
        
      }];
    
    return result;
}

//查询消息提醒设置信息
+ (RemindSet *)queryMessageRemindSetting
{
    _queue = [[XMPPDBManager sharedInstance] achieveQueue];
    __block RemindSet *remindSet;
    [_queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        NSArray *array = [RemindSet queryInDatabase:db];
        remindSet = [array lastObject];
    }];
    
    return remindSet;
}

//更新消息提醒设置
+ (BOOL)updateMessageRemindSetting:(RemindSet *)remindSet
{

    _queue = [[XMPPDBManager sharedInstance] achieveQueue];
    __block BOOL result;
    [_queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        result = [remindSet updateInDatabase:db];
    }];
    
    return result;

}

//根据群/讨论组ID查询群/讨论组消息提醒信息
+ (MessageRemind *)queryMessageRemindWithGroupID:(NSString *)groupID
{
 
    _queue = [[XMPPDBManager sharedInstance] achieveQueue];
    __block MessageRemind *messageRemind;
    [_queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        messageRemind = [MessageRemind queryGroupRemindWithGroupID:groupID database:db];
    }];
    
    return messageRemind;

}

@end
